<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="原编码" prop="cdCode">
        <el-input
          v-model="queryParams.cdCode"
          placeholder="请输入原编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域名称" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder="请输入区域名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开采深度" prop="kcDepth">
        <el-input
          v-model="queryParams.kcDepth"
          placeholder="请输入开采深度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开采方法" prop="kcWay">
        <el-input
          v-model="queryParams.kcWay"
          placeholder="请输入开采方法"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['define:sinkDefine:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['define:sinkDefine:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['define:sinkDefine:remove']"
          >删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="sinkDefineList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="原编码" align="center" prop="cdCode"  width="150"/>
      <el-table-column label="测点编码" align="center" prop="remark"  width="150"/>
      <el-table-column label="区域名称" align="center" prop="areaName"  width="150"/>
      <el-table-column label="开采深度" align="center" prop="kcDepth" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.kcDepth }} <span class="price">m</span></span>
        </template>
      </el-table-column>
      <el-table-column label="区域面积" align="center" prop="areaMj"  width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.areaMj }} <span class="price">k㎡</span></span>
        </template>
      </el-table-column>
      <el-table-column label="采区边界" align="center" prop="cqBoundary"  width="150"/>
      <el-table-column label="开采方法" align="center" prop="kcWay"  width="150"/>
      <el-table-column label="桩点编号" align="center" prop="zdNumber"  width="150"/>
      <el-table-column label="桩点坐标X" align="center" prop="zdX"  width="150"/>
      <el-table-column label="桩点坐标Y" align="center" prop="zdY"  width="150"/>
      <el-table-column label="桩点坐标Z" align="center" prop="zdZ"  width="150"/>
      <el-table-column
        label="设置时间"
        align="center"
        prop="settingTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.settingTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              size="small"
              type="success"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['define:sinkDefine:edit']"
            >
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['define:sinkDefine:remove']"
            >
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改地沉陷监测基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="原编码" prop="cdCode">
          <el-input v-model="form.cdCode" placeholder="请输入原编码" />
        </el-form-item>
        <el-form-item label="区域名称" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="开采深度" prop="kcDepth">
          <el-input
            v-model="form.kcDepth"
            placeholder="请输入开采深度"
            style="width:80%"
            @input="inputnum"
          />
          <span class="unit">m</span>
        </el-form-item>
        <el-form-item label="区域面积" prop="areaMj">
          <el-input
            v-model="form.areaMj"
            placeholder="请输入区域面积"
            style="width:80%"
            @input="inputnum"
          />
          <span class="unit">k㎡</span>
        </el-form-item>
        <el-form-item label="采区边界" prop="cqBoundary">
          <el-input
            v-model="form.cqBoundary"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="开采方法" prop="kcWay">
          <el-input v-model="form.kcWay" placeholder="请输入开采方法" />
        </el-form-item>
        <el-form-item label="桩点编号" prop="zdNumber">
          <el-input v-model="form.zdNumber" placeholder="请输入桩点编号" />
        </el-form-item>
        <el-form-item label="桩点坐标X" prop="zdX">
          <el-input v-model="form.zdX" placeholder="请输入桩点坐标X" />
        </el-form-item>
        <el-form-item label="桩点坐标Y" prop="zdY">
          <el-input v-model="form.zdY" placeholder="请输入桩点坐标Y" />
        </el-form-item>
        <el-form-item label="桩点坐标Z" prop="zdZ">
          <el-input v-model="form.zdZ" placeholder="请输入桩点坐标Z" />
        </el-form-item>
        <el-form-item label="设置时间" prop="settingTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.settingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择设置时间"
            style="width:100%"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSinkDefine,
  getSinkDefine,
  delSinkDefine,
  addSinkDefine,
  updateSinkDefine
} from "@/api/define/sinkDefine";

export default {
  name: "SinkDefine",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 地沉陷监测基础信息表格数据
      sinkDefineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 设置时间时间范围
      daterangeSettingTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mineCode: null,

        mineName: null,

        cdCode: null,

        areaName: null,

        kcDepth: null,

        areaMj: null,

        cqBoundary: null,

        kcWay: null,

        zdNumber: null,

        zdX: null,

        zdY: null,

        zdZ: null,

        settingTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cdCode: [
          { required: true, message: "原编码不能为空", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],

        areaName: [
          { required: true, message: "区域名称不能为空", trigger: "blur" }
        ],

        kcDepth: [
          { required: true, message: "开采深度不能为空", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        areaMj: [
          { required: true, message: "区域面积不能为空", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        cqBoundary: [
          { required: true, message: "采区边界不能为空", trigger: "blur" }
        ],

        kcWay: [
          { required: true, message: "开采方法不能为空", trigger: "blur" }
        ],

        zdNumber: [
          { required: true, message: "桩点编号不能为空", trigger: "blur" }
        ],

        zdX: [
          { required: true, message: "桩点坐标X不能为空", trigger: "blur" }
        ],

        zdY: [
          { required: true, message: "桩点坐标Y不能为空", trigger: "blur" }
        ],

        zdZ: [
          { required: true, message: "桩点坐标Z不能为空", trigger: "blur" }
        ],

        settingTime: [
          { required: true, message: "设置时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 限制只能在输入框中输入小数点后两位
    inputnum() {
      this.form.kcDepth = this.form.kcDepth.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.areaMj = this.form.areaMj.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
    },
    /** 查询地沉陷监测基础信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (
        null != this.daterangeSettingTime &&
        "" != this.daterangeSettingTime
      ) {
        this.queryParams.params[
          "beginSettingTime"
        ] = this.daterangeSettingTime[0];
        this.queryParams.params[
          "endSettingTime"
        ] = this.daterangeSettingTime[1];
      }
      listSinkDefine(this.queryParams).then(response => {
        this.sinkDefineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        mineCode: null,

        mineName: null,

        cdCode: null,

        areaName: null,

        kcDepth: null,

        areaMj: null,

        cqBoundary: null,

        kcWay: null,

        zdNumber: null,

        zdX: null,

        zdY: null,

        zdZ: null,

        settingTime: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSettingTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加地沉陷监测基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSinkDefine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改地沉陷监测基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSinkDefine(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSinkDefine(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除当前选中数据？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delSinkDefine(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>
